package com.carrentalplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carrentalplatform.model.Evaluation;
import com.carrentalplatform.service.EvaluationService;
import com.carrentalplatform.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 评价控制器
 */
@RestController
@RequestMapping("/api/evaluation")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    /**
     * 添加评价
     */
    @PostMapping("/add")
    public Result<Boolean> addEvaluation(@RequestBody @Valid Evaluation evaluation) {
        boolean success = evaluationService.addEvaluation(evaluation);
        if (success) {
            return Result.success(true);
        } else {
            return Result.fail("评价提交失败");
        }
    }

    /**
     * 获取用户的评价列表
     */
    @GetMapping("/user/{userId}")
    public Result<IPage<Evaluation>> getUserEvaluations(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        IPage<Evaluation> evaluations = evaluationService.getUserEvaluations(userId, page, size);
        return Result.success(evaluations);
    }

    /**
     * 获取车行的评价列表
     */
    @GetMapping("/shop/{shopId}")
    public Result<IPage<Evaluation>> getShopEvaluations(
            @PathVariable Long shopId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        IPage<Evaluation> evaluations = evaluationService.getShopEvaluations(shopId, page, size);
        return Result.success(evaluations);
    }

    /**
     * 获取订单的评价
     */
    @GetMapping("/order/{orderId}")
    public Result<Evaluation> getOrderEvaluation(@PathVariable Long orderId) {
        Evaluation evaluation = evaluationService.getOrderEvaluation(orderId);
        if (evaluation != null) {
            return Result.success(evaluation);
        } else {
            return Result.success(null); // 返回空而不是失败，因为可能确实没有评价
        }
    }

    /**
     * 获取车行平均评分
     */
    @GetMapping("/shop/{shopId}/score")
    public Result<Double> getShopAvgScore(@PathVariable Long shopId) {
        Double avgScore = evaluationService.calculateShopAvgScore(shopId);
        return Result.success(avgScore);
    }
} 